import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import javax.print.Doc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

public class Test1 {
    public static void main( String args[] ){
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient("192.168.1.29", 37019 );

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            // 创建集合
            mongoDatabase.createCollection("test");

            // 获取集合
            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合：" + collection);

            // 写入文档
            Random r = new Random();
            List<Document> docs = new ArrayList<Document>();
            for(int i = 0; i < 10; i++) {
                Document doc = new Document();
                doc.append("name", "name" + i);
                doc.append("age", r.nextInt(50));
                docs.add(doc);
                // collection.insertOne(doc); // 单一写入
            }
            collection.insertMany(docs);  // 批量写入

            // 查询所有
            FindIterable<Document> result = collection.find();
            MongoCursor<Document> cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("find all : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

            // 更新文档
            collection.updateMany(Filters.eq("name", "name0"), new Document("$set", new Document("name", "name100")));
            result = collection.find();
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("update find all : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

            // 删除文档
            collection.deleteOne(Filters.eq("name", "name100"));
            collection.deleteMany(Filters.lt("age", "30"));
            result = collection.find();
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("delete find all : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

            // 条件查询
            // 查询name = name3的
            result = collection.find(Filters.eq("name", "name3"));
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("eq : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }
            // 查询age > 30的
            result = collection.find(Filters.gt("age", 30));
            // result = collection.find(new Document("age", new Document("$gt", 30)));
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("gt : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }
            // 查询name以n开头，以9结尾的
            result = collection.find(Filters.regex("name", Pattern.compile("^n.*9$")));
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("regex : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

            // 查询name = name0 并且 age < 50的
            result = collection.find(Filters.and(Filters.eq("name", "name0"), Filters.lt("age", 50)));
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("and query : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

            // 查询name = name0 或者 age < 50的
            result = collection.find(Filters.or(Filters.eq("name", "name0"), Filters.lt("age", 50)));
            cursor = result.iterator();
            while(cursor.hasNext()){
                Document doc = cursor.next();
                System.out.print("or query : [ { doc : " + doc + " }, ");
                System.out.print("{ name : " + doc.getString("name") + " }, ");
                System.out.println("{ age : " + doc.get("age") + " } ]");
            }

        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}
